<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <title>tiptap content 文档命令 - itxst.com</title>
    <meta name="keywords" content="tiptap content 文档命令" />
    <meta name="description" content="tiptap content 文档命令包含clearContent 清除文档、insertContent 插入内容、insertContentAt 插入制定位置内容、setContent 设置新的内容等核心命令。" />
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <meta http-equiv="Content-Language" content="zh-CN">
    <link rel="shortcut icon" href="/img/favicon.ico" type="image/x-icon" />
    <link href="/css/zt.css?ver=1.6.6" rel="stylesheet" type="text/css" media="all" />
    <script type="text/javascript" language="javascript" src="/js/jquery.js?ver=1.6.6"></script>
    <script type="text/javascript" src="/js/highlight/highlight.min.js?ver=1.6.6"></script>
    <link rel="stylesheet" type="text/css" href="/js/highlight/styles/vs2015x.css?ver=1.6.6">
    <script type="text/javascript" language="javascript" src="/js/comm.js?ver=1.6.6"></script>
    <script type="text/javascript" language="javascript" src="/js/zt.js?ver=1.6.6"></script>
    <script type="text/javascript" charset="utf-8" src="/js/layer/layer.js?ver=1.6.6"></script>
    <link rel="alternate" media="only screen and(max-width: 640px)" href="http://m.itxst.com/tiptap/rvvain7z.html" >
    <!--[if lte IE 9]>
    <script src="/js/respond/respond.js?ver=1.6.6"></script>
    <![endif]-->
</head>
<body>
    <div class="body"  >
        <div class="header">
            <div class="naver">
                <div class="logo">
                    <a href="/"><img src="/img/logov31.png" /></a>
                </div>
                <div class="naver-left">
                 <a href="/tiptap/tutorial.html">tiptap 中文文档</a>
                </div>
                <div class="naver-search">
                    <div class="naver-search-txt">
                        <input type="text" id="keyword" placeholder="Please enter the keyword" data-topic="tiptap">
                    </div>
                    <div class="naver-search-btn"><a href="javascript:void(0)" onclick="Search()">Search</a></div>
                </div>
                 <div class="naver-right"> 
                        <a href="/ag-grid/tutorial.html">AG-GRID</a> 
                        <a href="/tiptap/tutorial.html">Tiptap</a> 
                        <a href="/css-flex/tutorial.html">CSS-FLEX</a> 
                        <a href="/Bootstrap-Table/QuickStart.html">Bootstrap-Table</a> 
                </div>
            </div>
            <div class="path">
                
            </div>
        </div>
        <div class="clearfix"></div>
        <div class="pagew">
        <div class="page" >
            <div class="page-body">
                <div class="content-detail">
                    <h1>tiptap content 文档命令</h1>
               
               <p class="pe"><strong>tiptap content</strong> 文档命令包含<strong>clearContent</strong> 清除文档、<strong>insertContent</strong> 插入内容、<strong>insertContentAt</strong> 插入制定位置内容、<strong>setContent</strong> 设置新的内容等核心命令。</p><h2>clearContent 清除内容</h2><p>清除编辑器里面的内容，但在运行该命令之后，编辑器文档也将至少有一个（空）段落</p><pre class="brush:js;toolbar:false"><code class="language-javascript">// 移除编辑器文档内容
editor.commands.clearContent()

//移除编辑器文档内容，并触发 update 事件
editor.commands.clearContent(true)</code></pre><h2>insertContent 插入内容</h2><p class="pe">插入<strong>node</strong>节点或<strong>html字符串</strong>到当前光标位置</p><pre class="brush:js;toolbar:false"><code class="language-javascript">// 插入纯文本
editor.commands.insertContent('Example Text')

// 插入HTML字符串
editor.commands.insertContent('&lt;h1&gt;Example Text&lt;/h1&gt;')

// 插入字符串去除两边空格
editor.commands.insertContent(' &lt;h1&gt;Example Text &lt;/h1&gt; ', 
{
  parseOptions: {
    preserveWhitespace: false,
  }
})

// 插入 Json节点
editor.commands.insertContent({
  type: 'heading',
  attrs: {
    level: 1,
  },
  content: [
    {
      type: 'text',
      text: 'Example Text',
    },
  ],
})

// 插入多个节点
editor.commands.insertContent([
  {
    type: 'paragraph',
    content: [
      {
        type: 'text',
        text: 'First paragraph',
      },
    ],
  },
  {
    type: 'paragraph',
    content: [
      {
        type: 'text',
        text: 'Second paragraph',
      },
    ],
  },
])</code></pre><h2>insertContentAt 插入制定位置</h2><p>insertContentAt 插入内容到制定位置或区间，参数如下：<br/></p><ul class=" list-paddingleft-2"><li><p class="pe"><strong>position</strong> number | Range 位置或区间</p></li><li><p class="pe"><strong>value</strong> 插入的内容字符串或JSON节点</p></li><li><p class="pe"><strong>options</strong> {updateSelection,parseOptions}&nbsp;<a target="_blank" href="https://prosemirror.net/docs/ref/#model.ParseOptions">详情</a></p></li></ul><pre class="brush:js;toolbar:false"><code class="language-javascript">editor.commands.insertContentAt(12, '&lt;p&gt;www.itxst.com&lt;/p&gt;', {
  updateSelection: true,
  parseOptions: {
    preserveWhitespace: 'full',
  }
})</code></pre><h2>setContent 覆盖内容</h2><p class="pe"><strong>setContent</strong> 回完全覆盖编辑器的内容，不保留原来的内容，可以是<strong>html</strong>、<strong>json</strong>字符串,参数如下：</p><div class="xst-table"><table interlaced="enabled"><tbody><tr class="ue-table-interlace-color-single firstRow"><th style="border-color: rgb(221, 221, 221); word-break: break-all;">参数</th><th style="border-color: rgb(221, 221, 221); word-break: break-all;">说明</th></tr><tr class="ue-table-interlace-color-double"><td valign="top" style="border-color: rgb(221, 221, 221); word-break: break-all;">content:string</td><td valign="top" style="border-color: rgb(221, 221, 221); word-break: break-all;">JSON 或 HTML</td></tr><tr class="ue-table-interlace-color-single"><td valign="top" style="border-color: rgb(221, 221, 221); word-break: break-all;">emitUpdate?: Boolean (false)</td><td valign="top" style="border-color: rgb(221, 221, 221); word-break: break-all;">是否触发 update 事件，默认为 false</td></tr><tr class="ue-table-interlace-color-double"><td valign="top" style="border-color: rgb(221, 221, 221); word-break: break-all;">parseOptions?: Record&lt;string, any&gt;</td><td valign="top" style="border-color: rgb(221, 221, 221);"><a target="_blank" href="https://prosemirror.net/docs/ref/#model.ParseOptions">点击查看</a></td></tr></tbody></table></div><pre class="brush:js;toolbar:false"><code class="language-javascript">// HTML
editor.commands.setContent('&lt;p&gt;Example Text&lt;/p&gt;')

// JSON
editor.commands.setContent({
  "type": "doc",
  "content": [
    {
      "type": "paragraph",
      "content": [
        {
          "type": "text",
          "text": "Example Text"
        }
      ]
    }
  ]
})</code></pre>
 
               <h2>
    下载教程 Demo
</h2>
<p class="pe">
   本教程<b>Demo</b>的源码点击这里下载，<a href="https://img1.itxst.com/8/c/8c32fcdc12cb8847c62e405be8a73835.zip" target="_blank" textvalue="Tiptap Demo">Tiptap Demo</a>，下载完成后运行<b>npm i</b>初始化依赖包。
</p>
                </div>
                <div class="content-r">   <a href="/tiptap/tutorial.html" target="_self">
                     <img src="https://img1.itxst.com/0/9/09355b3592a0c16799b504a9f2a70303.png" alt="tiptap 中文文档" />
                    </a>                                                <a href="//www.itxst.com/vue-draggable/tutorial.html" target="_blank">
                                <img src="//img1.itxst.com/b/f/bf9af8368dfac84cd703ef54e1e89d14.png" alt="vue.draggable" />
                            </a>
 
                </div>
            </div>
            <div class="home-menu scroll">
                <div class="tree">
      <a data-level="1" class="leve-1   doc-1   " href="/tiptap/tutorial.html" title="tiptap 中文文档">快速入门</a>
      <a data-level="1" class="leve-1    arrow-1" href="/tiptap/configuration.html" title="Guide 向导">Guide 向导</a>
      <a data-level="1" class="leve-1    arrow-2" href="/tiptap/api.html" title="tiptap api 列表">API 列表</a>
      <a data-level="2" class="leve-2   doc-1   " href="/tiptap/methods.html" title="tiptap editor 方法">tiptap 方法</a>
      <a data-level="2" class="leve-2   doc-1   " href="/tiptap/attrs.html" title="tiptap 属性">tiptap 属性</a>
      <a data-level="2" class="leve-2   doc-1   " href="/tiptap/settings.html" title="titap 配置">titap 配置</a>
      <a data-level="2" class="leve-2    arrow-2" href="/tiptap/uaqmeemr.html" title="tiptap commands 命令">commands 命令</a>
      <a data-level="3" class="leve-3   doc-1   " href="/tiptap/uaqmeemr.html" title="tiptap commands 命令介绍">commands 命令介绍</a>
      <a data-level="3" class="leve-3   doc-1    doc-2 " href="/tiptap/rvvain7z.html" title="tiptap content 文档命令">content 文档命令</a>
      <a data-level="3" class="leve-3   doc-1   " href="/tiptap/3qbv63ii.html" title="tiptap nodes &amp; marks 命令大全">nodes &amp; marks 命令</a>
      <a data-level="3" class="leve-3   doc-1   " href="/tiptap/ueriebja.html" title="tiptap lists 命令">lists 命令</a>
      <a data-level="3" class="leve-3   doc-1   " href="/tiptap/selection.html" title="tiptap selection 命令">selection 命令</a>
      <a data-level="2" class="leve-2    arrow-1" href="/tiptap/363qzvqv.html" title="nodes 节点">nodes 节点</a>
      <a data-level="2" class="leve-2    arrow-1" href="/tiptap/marks.html" title="tiptap marks 标记">marks 标记</a>
      <a data-level="2" class="leve-2    arrow-1" href="/tiptap/extensions.html" title="tiptap extensions 扩展">extensions 扩展</a>
                 </div>
                 <div  class="clearfix"></div>
            </div>
        </div>
        <div class="clearfix"></div>
           <div class="footer">
© 2023 itxst.com All rights reserved.
</div>

<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?7e15ed96eb8133218e99de8bc1f5cb61";
  var s = document.getElementsByTagName("script")[0];
  s.parentNode.insertBefore(hm, s);
})();
</script>
       </div>
    </div> 
</body>
</html>
